package controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.SimpleFormController;

import service.SimpleService;
import vo.SimpleObjVo;

public class SimpleController {
	
	private SimpleService simpleService;
	
	public void setSimpleService(SimpleService simpleService){
		this.simpleService= simpleService;
	}
	

	@RequestMapping(value="/home.do")
	public ModelAndView home(HttpServletRequest reqs,
			HttpServletResponse resp) throws Exception {
				
		return getDataList(reqs);
	}
	
	@RequestMapping(value="/update.do")
	public ModelAndView update(HttpServletRequest reqs,
			HttpServletResponse resp,  @RequestParam("rd") String id) throws Exception {
		
		SimpleObjVo vo = new SimpleObjVo();
		vo.setId(id);
		SimpleObjVo obj = simpleService.select(vo);
		reqs.setAttribute("obj", obj);
		reqs.setAttribute("job", "update");
		ModelAndView model = new ModelAndView();
		model.setViewName("./WEB-INF/jsp/edit.jsp");
		return model;
	}
	
	@RequestMapping(value="/delete.do")
	public ModelAndView delete(HttpServletRequest reqs,
			HttpServletResponse resp,  @RequestParam("rd") String id) throws Exception {
		
		SimpleObjVo vo = new SimpleObjVo();
		vo.setId(id);
		simpleService.delete(vo);
		
		return getDataList(reqs);
	}
	
	@RequestMapping(value="/insert.do")
	public ModelAndView insert(HttpServletRequest reqs) throws Exception {
		
		reqs.setAttribute("job", "insert");
		ModelAndView model = new ModelAndView();
		model.setViewName("./WEB-INF/jsp/edit.jsp");
		return model;
	}
	
	@RequestMapping(value="/do_update.do")
	public ModelAndView do_update(HttpServletRequest reqs,
			HttpServletResponse resp,  @RequestParam("id") String id, 
			@RequestParam("user_id") String user_id) throws Exception {
		
		SimpleObjVo vo = new SimpleObjVo();
		vo.setId(id);
		vo.setUser_id(user_id);
		simpleService.update(vo);
	
		return getDataList(reqs);
	}
	@RequestMapping(value="/do_insert.do")
	public ModelAndView do_insert(HttpServletRequest reqs,
			HttpServletResponse resp, 
			@RequestParam("user_id") String user_id) throws Exception {
		
		SimpleObjVo vo = new SimpleObjVo();
		vo.setUser_id(user_id);
		simpleService.insert(vo);
	
		return getDataList(reqs);
	}
	public ModelAndView getDataList(HttpServletRequest reqs){
		// get the object list from db
		SimpleObjVo obj = new SimpleObjVo();
		List<SimpleObjVo> objList = simpleService.selectList(obj);
		reqs.setAttribute("objList", objList);
		ModelAndView model = new ModelAndView();
		model.setViewName("./WEB-INF/jsp/home.jsp");
		return model;
	}
	// scheduler action
	public void insertBySchedule(){
		// "0 0/5 14 * * ?"
		
		// make vo
		SimpleObjVo obj = new SimpleObjVo();
		obj.setUser_id("spring_scheduler");
						
		// insert vo
		simpleService.insert(obj);
	}

}
